package com.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.bean.Activity;
import com.club.service.ActivityService;
import com.club.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("activity")
@CrossOrigin
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @GetMapping("list")
    public Result getList(){
        List<Activity> list=activityService.list();
        return Result.success(list);
    }
    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(value = "searchText",required = false) String searchText){
        System.out.println(searchText);
        IPage<Activity> result=null;
        if(searchText==null||searchText.trim().isEmpty()) {
            result=activityService.page(page);
        }else{
            QueryWrapper<Activity> wrapper= new QueryWrapper<>();
            wrapper.like("actId",searchText)
                    .or().like("actName",searchText)
                    .or().like("organizerId",searchText)
                    .or().like("startTime",searchText)
                    .or().like("endTime",searchText)
                    .or().like("location",searchText)
                    .or().like("description",searchText)
                    .or().like("maxPpl",searchText)
                    .or().like("actualPpl",searchText);
            result=activityService.page(page,wrapper);
        }
        return Result.success(result);
    }
    //添加管理员
    @PostMapping("add")
    public Result add(@RequestBody Activity actId){
        activityService.save(actId);
        Activity act1=activityService.getById(actId.getActId());
        return Result.success("保存成功",actId);
    }
    //删除管理员
    @DeleteMapping("/{actId}")
    public Result delete(@PathVariable("actId") String actId){
        boolean b=activityService.removeById(actId);
        if(b){
            return Result.success("删除成功");
        }else{
            return Result.error(201,"删除失败");
        }
    }
    @PutMapping("edit")
    //修改管理员信息
    public Result edit(@RequestBody Activity actId){
        boolean b= activityService.saveOrUpdate(actId);
        //保存管理员信息
        Activity act1=activityService.getById(actId.getActId());
        if(b){
            return Result.success("修改成功",act1);
        }else {
            return Result.error(201,"修改失败");
        }
    }
}
